<template>
    <el-select v-model="localValue" placeholder="请选择" @change="change">
        <el-option v-for="item in optionNew" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
</template>

<script>
import optionData from '@/utils/optionData'
export default {
    name: 'SelectForm',
    props: {
        code: {
            type: String
        },
        defaultValue: {
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            option: optionData,
            localValue: ''
        }
    },
    computed: {
        optionNew() {
            return this.option.filter(item => item.code === this.code) || []
        }
    },
    created() {
        if (this.defaultValue) {
            this.localValue = this.defaultValue
            this.change(this.localValue)
        }
    },
    methods: {
        change(value) {
            if (value) {
                const { label, name } = this.getLabel(value)
                this.$emit('select-change', value, { value, label, name })
            } else {
                this.$emit('select-change', value)
            }
        },
        getLabel(value) {
            return this.optionNew.filter(item => item.value == value)[0]
        }
    }
}
</script>

<style></style>
